package com.ruoyi.web.controller.information;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.information.domain.InterviewPosition;
import com.ruoyi.information.domain.InterviewDimension;
import com.ruoyi.information.domain.InterviewQuestion;
import com.ruoyi.information.service.*;
import com.ruoyi.web.util.ChatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/information/question")
public class InterviewQuestionController {

    // 岗位
    @Autowired
    private IInterviewPositionService interviewPositionService;
    // 面试维度
    @Autowired
    private IInterviewDimensionService interviewDimensionService;
    // 面试记录
    @Autowired
    private IInterviewRecordService interviewRecordService;
    // 面试问题
    @Autowired
    private IInterviewQuestionService iInterviewQuestionService;

    // 问题数量
    private static final int QUESTION_NUMBER = 5;
            /**
             * 生成模拟面试的问题
             * @param positionId 岗位id
             * @return 生成的问题列表
             */
    @GetMapping("/genQuestion")
    public AjaxResult genQuestion(Long positionId){
        // 1、根据前端传过来的岗位id查询岗位信息
        InterviewPosition interviewPosition = interviewPositionService.selectInterviewPositionById(positionId);
        // 2、查询岗位对应的维度信息
        List<InterviewDimension> dimensionList = interviewDimensionService.selectDimensionByPositionId(positionId);
        String dimensions = genDimensionPrompt(dimensionList);
        // 2、根据岗位的名称生成模拟面试的问题
        IQuestionGenerateService service = ChatUtil.getService(IQuestionGenerateService.class);
        String message = service.generateQuestionByPosition(interviewPosition.getName(), QUESTION_NUMBER, dimensions);
        System.out.println("Generated Message: " + message); // 打印实际生成的 message
        // 3、生成一次面试记录，解析这次记录的问题，保存到数据库中
        Long recordId = interviewRecordService.generateRecord(positionId,message);
        return AjaxResult.success(recordId);
    }

    /**
     * 根据维度列表生成维度提示词    [维度1(权重)]
     * @param dimensionList
     * @return
     */
    private String genDimensionPrompt(List<InterviewDimension> dimensionList) {
        return dimensionList.stream().map(dimension -> {
            return "[" + dimension.getName() + "("+ dimension.getWeight() +")]";
        }).collect(Collectors.joining("、"));
    }

    /**
     * 根据面试记录查询问题列表
     * @param recordId
     * @return
     */
    @GetMapping("/getQuestions")
    public AjaxResult getQuestions(Long recordId){
        InterviewQuestion queryVo = new InterviewQuestion();
        queryVo.setRecordId(recordId);
        List<InterviewQuestion> interviewQuestions = iInterviewQuestionService.selectInterviewQuestionList(queryVo);
        return AjaxResult.success(interviewQuestions);
    }
}
